package com.leetcode.LeetCode精选TOP面试题;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 覃国强
 * @date 2022-05-09 16:15
 */
public class 题库_0003_无重复字符的最长子串 {

  static
  class Solution {
    public int lengthOfLongestSubstring(String s) {
      if (s == null || s.length() == 0) {
        return 0;
      }
      int left = -1;
      Map<Character, Integer> map = new HashMap<>();
      char[] chars = s.toCharArray();
      int answer = 0;
      for (int i = 0; i < chars.length; ++i) {
        left = Math.max(map.getOrDefault(chars[i], left), left);
        answer = Math.max(answer, i - left);
        map.put(chars[i], i);
      }
      return answer;
    }
  }

}
